//canvas创建等相关操作
_dss_editor._canvas._operation = {};
_dss_editor._canvas._operation.viewOp = {};
_dss_editor._canvas._operation.slideOp = {};
_dss_editor._canvas._operation.uioOp = {};
_dss_editor._canvas._operation.buildTab = function(presenterName, canvasType) {
    var id = presenterName;
    var that = this;
    id = that.createId(id);
    if (id.indexOf(' ') > 0) {
        var ss = id.split(' ');
        id = '';
        for (var i in ss) {
            id.concat(ss[i]);
        }
    }
    $('#canvasTabs').tabs('add', {
        title : presenterName,
        content : that.createCanvasEntity(id),
        closable : true,

    });
   
    var canvas = this.createCanvasViewer(id, presenterName, canvasType);
    _dss_editor.activeCanvas = presenterName;
    //initCommandBotton(presenterName);
    return canvas;
};

_dss_editor._canvas._operation.initCommandBotton = function(presenterName, canvasType) {
    var p = $('#canvasTabs').tabs().tabs('getTab', presenterName);
    var mb = p.panel('options').tab.find('a.tabs-inner');
    //	document.body.appendChild(mc);
    var mc = $C('div');
    var ca = $C('canvas');
    mc.id = 'm_' + presenterName;
    mc.style.width = '200px';
    mc.style.height = '200px';
    mc.className = 'menu-content';
    ca.id = 'cc_' + presenterName;
    mc.appendChild(ca);
    var node = $F('mm');
    document.body.appendChild(mc);
    mb.splitbutton({
        menu : '#m_' + presenterName,
        iconAlign : 'left'
    });
    var canvas = this.createCanvasViewer(ca.id, presenterName, canvasType);
};

_dss_editor._canvas._operation.buildWindow = function(title, canvasType) {
    var id = title;
    var win = $C("div");
    var that = this;
    id = that.createId(id);
    win.id = "window" + id;
    var parentNode = $F("canvas");
    parentNode.appendChild(win);
    $('#' + win.id).window({
        width : 0,
        height : 0,
        title : title,
        minimizable : false,
        inline : true,
        content : that.createCanvasEntity(id),
        onClose : function() {
            _dss_editor.canvas[title] = null;
        },
        onResize : function() {
            this.matchCanvas(title);
        },
        onExpand : function() {
            this.matchCanvas(title);
        },
        onMove : function(left, top) {
            this.onMoveWindow(win.id, left, top);
        },
        onOpen : function() {
            console.log('open');
            t(win.id);
        }
    });

    var canvas = createCanvasViewer(id, title, canvasType);
    _dss_editor.activeCanvas = title;
    //_dss_editor.canvas.push(canvas);
    return canvas;
};

_dss_editor._canvas._operation.buildArrayWindow = function(array, parentCanvas) {
    var title = array.name;
    var that = this;
    var id = that.createId(array.name);
    var win = $C("div");
    win.id = "array" + id;
    var parentNode = $F('canvas');
    //window method
    // var parentNode = $('#canvasTabs').tabs('getTab',parentCanvas)[0];
    //tab method
    console.log(parentNode.content);
    var rect = array.subInstance[0].getRectangle();
    var height = rect.h + 120;
    var parent = array.parent;
    parentNode.appendChild(win);
    $('#' + win.id).window({
        //rect : array.subInstance[0].getRectangle(),
        width : 500,
        height : height,
        title : title + '  ---  [Array tools]',
        minimizable : false,
        inline : true,
        modal : true,
        content : that.createCanvasEntity(id),
        onBeforeClose : function() {
            if (array.subInstance.length > 0) {
                array.subInstance[0].setRectangle(rect);
                array.parent = parent;
                ArrayTool.gather(array, parentCanvas);
            } else {
                var sys = _dss_editor.canvas[id].sys.getChildren()[0];
                parent.removeChild(array);
                if (sys != null) {
                    sys.setRectangle(rect);
                    parent.addChild(sys);
                }
                _dss_editor.canvas[parentCanvas].update();
                _dss_editor.canvas[parentCanvas].removeSelectionSystem();
            }

            _dss_editor.canvas[id] = null;
        },
        onClose : function() {

        },
        onResize : function() {
            that.matchCanvas(id);
        },
        onExpand : function() {
            that.matchCanvas(id);
        },
        onMove : function(left, top) {
            that.onMoveWindow(win.id, left, top);
        },
        tools : [{
            iconCls : 'icon-add',
            handler : function() {

            }
        }, {
            iconCls : 'icon-remove',
            handler : function() {

            }
        }, {
            iconCls : 'icon-array-gather',
            handler : function() {
                ArrayTool.gather(array, id);
            }
        }, {
            iconCls : 'icon-array-expand',
            handler : function() {
                ArrayTool.expand(array, id);
            }
        }]
    });
    var canvas = that.createCanvasViewer(id, id, _dss_editor.canvas[parentCanvas].type);
    canvas.sysForView = {
        complete : {
            sys : {
                content : {
                    composite : {
                        subArray : [array]
                    }
                }
            },
            links : []//_dss_editor._toolkit.getSystemAllInnerLinks(array)
        }
    };
    canvas.layout = function() {
        Layout.array_layout(array, this);
    };
    canvas.sys = canvas.sysForView.complete.sys;
    canvas.links = canvas.sysForView.complete.links;
    canvas.init(canvas.sysForView);
    //initSys(canvas.sys);
    canvas.layoutType = LAYOUT_ARRAY;
    canvas.matchSize();
    canvas.update();
    //	return canvasViewer;
};

_dss_editor._canvas._operation.matchCanvas = function(title) {
    var canvas = _dss_editor.canvas[title];
    if (canvas) {
        canvas.layout();
        canvas.matchSize();
        canvas.update();
    }
};

_dss_editor._canvas._operation.onMoveWindow = function(id, left, top) {
    if (top <= 0) {
        $('#' + id).window('move', {
            top : 10,
            left : left
        });
    }
};
/**
 * 通过esay ui 创建，只需要返回字符换即可
 * @param id
 */
_dss_editor._canvas._operation.createCanvasEntity = function(id) {
    id = this.createId(id);
    return '<div id="drillList' + id + '" style="position: fixed;;background:#F5F5DC;font-size:14px;width:100%"></div>' + '<canvas class="myCanvas" id = ' + id + '  width="1000px" height="1000px" contextmenu="mycontext" background=#eaf2ff >	your browser does not support the canvas tag </canvas>';
};

_dss_editor._canvas._operation.createCanvasViewer = function(id, presenterName, canvasType) {
    var canvas = _dss_editor._canvas.create();
    id = this.createId(id);
    canvas.entity = $F("" + id);
    canvasViewerEventInit(canvas);
    _dss_editor.canvas[presenterName] = canvas;
    canvas.presenterName = presenterName;
    canvas.type = canvasType;
    //init palette
    restoreAllFolder(canvas);
    return canvas;
};

_dss_editor._canvas._operation.createId = function(id) {
    if (id.indexOf('[') > 0) {
        id = _dss_editor._toolkit.strReplaceAll(id, '[', '');
    }
    if (id.indexOf(']') > 0) {
        id = _dss_editor._toolkit.strReplaceAll(id, ']', '');
    }
    if (id.indexOf(' ') > 0) {
        id = _dss_editor._toolkit.strReplaceAll(id, ' ', '');
    }
    if (id.indexOf('*') > 0) {
        id = _dss_editor._toolkit.strReplaceAll(id, '*', '_');
    }
    if (id.indexOf('.') > 0) {
        id = _dss_editor._toolkit.strReplaceAll(id, '.', '_');
    }
    return id;
}; 